//
//  UIBarButtonItem+Extension.swift
//  TJSinaWeibo
//
//  Created by 唐后能 on 16/4/12.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

extension UIBarButtonItem {

    // 类方法,创建带高亮图片的UIBarButtonItem
//    class func creatBarButtonItem(imgName:String,hightImgName:String)->UIBarButtonItem {
//        
//        let leftBtn = UIButton()
//        leftBtn.setBackgroundImage(UIImage(named: imgName), forState: UIControlState.Normal)
//        leftBtn.setBackgroundImage(UIImage(named: hightImgName), forState: UIControlState.Highlighted)
//        leftBtn.sizeToFit()
//        return UIBarButtonItem(customView: leftBtn)
//    }

    // 对象方法.创建带高亮图片的UIBarButtonItem
    convenience init(imgName:String,hightImgName:String?,target: AnyObject?, action: Selector) {
        
        let leftBtn = UIButton()
        leftBtn.setBackgroundImage(UIImage(named: imgName), forState: UIControlState.Normal)
        var hightName = "\(hightImgName)_highlighted"
        if hightImgName != nil {
            
            hightName = hightImgName!
        }
        leftBtn.setBackgroundImage(UIImage(named: hightName), forState: UIControlState.Highlighted)
        leftBtn.sizeToFit()
        // 添加target.
        leftBtn.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        
        self.init(customView:leftBtn)
    }
    
}